/**
 * _ooOoo_
 * o8888888o
 * 88" . "88
 * (| -_- |)
 * O\  =  /O
 * ____/`---'\____
 * .'  \\|     |//  `.
 * /  \\|||  :  |||//  \
 * /  _||||| -:- |||||-  \
 * |   | \\\  -  /// |   |
 * | \_|  ''\---/''  |   |
 * \  .-\__  `-`  ___/-. /
 * ___`. .'  /--.--\  `. . __
 * ."" '<  `.___\_<|>_/___.'  >'"".
 * | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 * \  \ `-.   \_ __\ /__ _/   .-` /  /
 * ======`-.____`-.___\_____/___.-`____.-'======
 * `=---='
 * 佛祖保佑       永无BUG
 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 * Copyright (c) 2016,  email:14902300@qq.com All Rights Reserved.
 */
package com.fclassroom.logger.entity;

import com.jason.framework.util.StringUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.concurrent.ArrayBlockingQueue;

/**
 * @author jason
 * @date 2016年10月21日 下午10:36:14
 */
public final class LogCache {
    protected static final Logger logger = LogManager.getLogger();

    /**扩展为请求队列*/
    private ArrayBlockingQueue<OperaterLog> operaterLogQueue;
    private ArrayBlockingQueue<SystemLog> systemLogQueue;

    private ArrayBlockingQueue<LogRLogin> logRLoginQueue;
    private ArrayBlockingQueue<LogRExamRead> logRExamReadQueue;

    public LogCache(int maxCapacity) {
        logRLoginQueue = new ArrayBlockingQueue<LogRLogin>(maxCapacity);
        logRExamReadQueue = new ArrayBlockingQueue<LogRExamRead>(maxCapacity);
        operaterLogQueue = new ArrayBlockingQueue<OperaterLog>(maxCapacity);
        systemLogQueue = new ArrayBlockingQueue<SystemLog>(maxCapacity);
    }
    public OperaterLog pullOperaterLog() {
        return this.operaterLogQueue.poll();
    }
    public LogRLogin pullLogRLogin() {
        return this.logRLoginQueue.poll();
    }
    public LogRExamRead pullLogRExamRead() {
        return this.logRExamReadQueue.poll();
    }

    public void putSystemLog(SystemLog log) {
        try {
            this.systemLogQueue.put(log);
        } catch (InterruptedException e) {
            logger.error("系统日志放入队列出错：" + e.getMessage());
        }
    }

    public void putOperaterLog(OperaterLog operaterLog) {
        try {
            this.operaterLogQueue.put(operaterLog);
            logger.info("put数据后，队列当前大小：" + operaterLogQueue.size());
        } catch (InterruptedException e1) {
            logger.error("操作日志放入队列出错：" + e1.getMessage());
        }

        //判断是否是登录操作
        if (StringUtil.isNotBlank(operaterLog.getOperateUrl()) && (operaterLog.getOperateUrl().indexOf("loginBy") > -1 || operaterLog.getOperateCategory().indexOf("用户访问") > -1)) {
            LogRLogin logRLogin = new LogRLogin(operaterLog);
            if (logRLogin != null && logRLogin.getUserId() != null && logRLogin.getUserId() != 0) {
                try {
                    logRLoginQueue.put(logRLogin);
                } catch (InterruptedException e) {
                    logger.error("登录日志放入队列出错：" + e.getMessage());
                }
            }
        }
        //读卷 operateCategory=读卷
        if ("读卷".equals(operaterLog.getOperateCategory()) && "上传读卷日志".equals(operaterLog.getOperateMethod())) {
            LogRExamRead examRead = new LogRExamRead(operaterLog);
            if (examRead != null && examRead.getExamId() != null) {
                try {
                    logRExamReadQueue.put(examRead);
                } catch (InterruptedException e) {
                    logger.error("读卷日志放入队列出错：" + e.getMessage());
                }
            }
            
        }
    }
}